package net.killerscape.model.player;

import net.killerscape.model.Player;
import net.killerscape.net.ActionSender;

public class Bank {
	public static int MAX_ITEMS = 350, FRAME = 5382;
	private Player player;
	private BankItem[] bank = new BankItem[MAX_ITEMS];
	
	public Bank(Player p) {
		player = p;
	}
	
	public void resetScreen() {
		player.getActionSender().clearItemsOnInterface(FRAME);
	}
	
	public int getAmount(int slot) {
		if(!itemInSlot(slot))
			return -1;
		return bank[slot].getAmount();
	}
	
	public boolean itemInSlot(int slot) {
		return bank[slot] != null;
	}
	
	public int getId(int slot) {
		if(!itemInSlot(slot))
			return -1;
		return bank[slot].getID();
	}
	
	public int addItem(int id, int amount) {
		int slot = -1;
		for(int i = 0; i < MAX_ITEMS; i++)
			if(!itemInSlot(i)) {
				slot = i;
				break;
			}
		if(slot == -1) return slot;
		bank[slot] = new BankItem(id,amount);
		ActionSender sender = player.getActionSender();
		sender.sendItem(id, amount, slot, FRAME);
		return slot;
	}
	
	public boolean removeItemAtSlot(int slot) {
		if(!itemInSlot(slot))
			return false;
		bank[slot] = null;
		return true;
	}
	
	public boolean removeItem(int id) {
		for(int i = 0; i < MAX_ITEMS; i++)
			if(itemInSlot(i))
				if(getId(i) == id) {
					removeItemAtSlot(i);
					return true;
				}
		return false;
	}
	
	public boolean playerHasItem(int id) {
		return playerHasItemAmount(id, 1);
	}
	
	public boolean playerHasItemAmount(int id, int amount) {
		int amtFound = 0;
		for(int i = 0; i < MAX_ITEMS; i++)
			if(itemInSlot(i))
				if(getId(i) == id) {
					amtFound += getAmount(i);
					if(amtFound >= amount)
						return true;
					else
						continue;
				}
		return false;
	}
	
	static class BankItem {
		private int itemID,itemAmount;
		
		public BankItem(int id, int amount) {
			itemID = id;
			itemAmount = amount;
		}
		
		public int getID() {
			return itemID;
		}
		
		public void setID(int id) {
			itemID = id;
		}
		
		public int getAmount() {
			return itemAmount;
		}
		
		public void setAmount(int amount) {
			itemAmount = amount;
		}
	}
}
